Headship = function() {
	return {
		// 设置body的高度为文档的高度
		doLayout : function() {
			$('body').height($(document).height());
		},

		initCompany : function() {
			$('#companyTree').tree({
				url       : '/pc/company/tree.htm',
				idField   : 'id',
				treeField : 'text',
				onClick   : function(node) {
					$('#headshipDataGrid').datagrid({
						title         : '企业【' + node.text + '】岗位列表',
						url           : '/pc/headship/all.htm',
						queryParams   : {
							'companyId' : node.id,
							'keyword'   : ""
						},
						pageNumber    : 1,
						toolbar       : '#toolBar'
					});
				}
			});
		},
		
		// 初始化表格
		initGrid : function() {
			$('#headshipDataGrid').datagrid({
				title           : '岗位列表',
				fit				: true,
				fitColumns		: true,
				striped			: true,
				rownumbers		: true,
				autoRowHeight	: true,
				nowrap			: true,
				pagination		: true,
				pageSize        : 20,
				pageList        : [10, 20, 30, 40, 50],
				method 			: 'POST',
				columns			: [[
				    {field : 'headshipId',        title : 'headshipId',  width : 100,   checkbox  : true},
				    {field : 'headshipName',      title : '岗位名称',     width : 100 },
				    {field : 'createMan',         title : '创建人',       width : 100,  align : 'center'},
				    {field : 'createTime',        title : '创建时间',     width : 100,  align : 'center'},
				    {field : 'modifyMan',         title : '修改人',       width : 100,  align : 'center'},
				    {field : 'modifyTime',        title : '修改时间',     width : 100,  align : 'center'},
				    {field : 'empNum',            title : '员工数量(人)', width : 100,  align : 'center'},
				    {field : 'companyName',       title : '所属企业',     width : 100,  align : 'center'}
				]],
				toolbar     : [{
					text    : '添加',
					iconCls : 'add',
					handler : function() {
						var row = $('#companyTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业!");
							return;
						} else {
							Ict.openWin("新增岗位信息", 400, 260, "/pc/headship/add.htm");
						}
					}
				}, {
					text    : '修改',
					iconCls : 'edit',
					handler : function() {
						var row = $('#companyTree').tree('getSelected');
						if(null == row || "" == row) {
							Ict.info("请选择企业!");
							return;
						} else {
							var rows = $('#headshipDataGrid').datagrid('getSelections');
							if (0 == rows.length) {
								Ict.info("请选择你要修改的岗位!");
								return;
							} else if (rows.length > 1) {
								Ict.info("请选择一条记录进行修改!");
								return;
							} else if (1 == rows.length) {
								Ict.openWin("修改岗位信息", 400, 260, "/pc/headship/edit.htm?headshipId=" + rows[0].headshipId);
							}
						}
					}
				}, {
					text    : '删除',
					iconCls : 'remove',
					handler : function() {
						Headship.remove();
					}
				}, {
					text    : '刷新',
					iconCls : 'reload',
					handler : function() {
						Headship.reload();
					}
				}]
			});
		},
		
		reload : function() {
			$('#headshipDataGrid').datagrid('reload');
		},
		
		remove : function() {
			var row = $('#companyTree').tree('getSelected');
			if(null == row || "" == row) {
				Ict.info("请选择企业!");
				return;
			}
			var rows = $('#headshipDataGrid').datagrid('getSelections');
			if(0 == rows.length) {
				Ict.info("请选择你要删除的岗位!");
				return;
			} else {
				for(var i = 0; i < rows.length; i++) {
					if(rows[i].empNum > 0) {
						Ict.info("请先删除岗位下的员工!");
						return;
					}
				}
				var idArray = "";
				for(var i = 0; i < rows.length; i++) {
					idArray += rows[i].headshipId + ",";
				}
				idArray = idArray.substring(0, idArray.length - 1);
				Ict.confirm("是否删除已选岗位?", function(yes) {
					if(yes) {
						$.post('/pc/headship/delHeadship.htm', {'headshipIds' : idArray}, function(data) {
							if('SUCCESS' == data) {
								Ict.info("删除成功");
								Headship.reload();
							} else {
								Ict.error("删除失败");
							}
						});
					}
				});
			}
		},
		
		// 在toolbar上面添加搜索框
		addSearchBox : function(width) {
			$('.searchbox').css({'width': width+'px'});
			$('.searchbox-text').css({'width': eval(width-20)+'px'});
			$('.datagrid-toolbar').append($('.serarchbox'));
			$('.datagrid-toolbar').append($('#tbar'));
		},
		
		doSearch : function(value) {
			$('#headshipDataGrid').datagrid({
				url           : '/pc/headship/all.htm',
				queryParams   : {
					'keyword' : $.trim(value)
				},
				pageNumber    : 1,
				toolbar       : '#toolBar'
			});
		},
		
		init : function() {
			Headship.initCompany();
			Headship.initGrid();
			Headship.doLayout();
			Headship.addSearchBox(250);
		}
	};
}();

$(function(){
	Headship.init();
});
